package constant.milk.toilet.dialog;

import constant.milk.toilet.R;
import constant.milk.toilet.util.MilkUtil;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MilkTimeDialog extends Dialog
{
	private Context context;
	private float TEXT_SIZE;
	
	private TextView titleTextView;
	
	private TextView hourPlusTextView;
	private TextView hourTextView;
	private TextView hourMinuseTextView;
	
	private TextView minPlusTextView;
	private TextView minTextView;
	private TextView minMinuseTextView;
	
	private TextView secPlusTextView;
	private TextView secTextView;
	private TextView secMinuseTextView;
	
	private TextView settingButton;
	private TextView cencelButton;
	
	private View contentView;
	
	private int hour;
	private int min;
	private int sec;
	
	private TimeCollBank collbank = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dialog_time);
		
		setSettingFontSize();
		
		setFindView();
		setOnClick();
		setData();
	}

	private void setFindView()
	{
		contentView = findViewById(R.id.dialogTimeContentView);
		titleTextView = (TextView) findViewById(R.id.dialogTimeTitleTextView);
		
		hourPlusTextView = (TextView) findViewById(R.id.dialogTimeHourPlusTextView);
		hourTextView = (TextView) findViewById(R.id.dialogTimeHourTextView);
		hourMinuseTextView = (TextView) findViewById(R.id.dialogTimeHourMinuseTextView);
		
		minPlusTextView = (TextView) findViewById(R.id.dialogTimeMinPlusTextView);
		minTextView = (TextView) findViewById(R.id.dialogTimeMinTextView);
		minMinuseTextView = (TextView) findViewById(R.id.dialogTimeMinMinuseTextView);
		
		secPlusTextView = (TextView) findViewById(R.id.dialogTimeSecPlusTextView);
		secTextView = (TextView) findViewById(R.id.dialogTimeSecTextView);
		secMinuseTextView = (TextView) findViewById(R.id.dialogTimeSecMinuseTextView);
		
		settingButton = (TextView) findViewById(R.id.dialogTimeSettingTextView);
		cencelButton = (TextView) findViewById(R.id.dialogTimeCencelTextView);
	}
	
	private void setOnClick()
	{
		hourPlusTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (hour > 22)
				{
					hour = 0;
				}
				else
				{
					hour = hour + 1;
				}
				
				hourTextView.setText(MilkUtil.getTime(hour));
				titleTextView.setText(MilkUtil.getTimeSec(hour, min, sec));
			}
		});
		
		hourMinuseTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (hour < 1)
				{
					hour = 23;
				}
				else
				{
					hour = hour - 1;
				}
				
				hourTextView.setText(MilkUtil.getTime(hour));
				titleTextView.setText(MilkUtil.getTimeSec(hour, min, sec));
			}
		});
		
		minPlusTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (min > 58)
				{
					min = 0;
				}
				else
				{
					min = min + 1;
				}
				
				minTextView.setText(MilkUtil.getTime(min));
				titleTextView.setText(MilkUtil.getTimeSec(hour, min, sec));
			}
		});
		
		minMinuseTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (min < 1)
				{
					min = 59;
				}
				else
				{
					min = min - 1;
				}
				
				minTextView.setText(MilkUtil.getTime(min));
				titleTextView.setText(MilkUtil.getTimeSec(hour, min, sec));
			}
		});
		
		secPlusTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (sec > 58)
				{
					sec = 0;
				}
				else
				{
					sec = sec + 1;
				}
				
				secTextView.setText(MilkUtil.getTime(sec));
				titleTextView.setText(MilkUtil.getTimeSec(hour, min, sec));
			}
		});
		
		secMinuseTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (sec < 1)
				{
					sec = 59;
				}
				else
				{
					sec = sec - 1;
				}
				
				secTextView.setText(MilkUtil.getTime(sec));
				titleTextView.setText(MilkUtil.getTimeSec(hour, min, sec));
			}
		});
		
		settingButton.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				collbank.getTime(hour, min, sec);
				dismiss();
			}
		});
		
		cencelButton.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				dismiss();
			}
		});
	}
	
	private void setData()
	{
		titleTextView.setTextSize(TEXT_SIZE);
		
		hourPlusTextView.setTextSize(TEXT_SIZE);
		hourTextView.setTextSize(TEXT_SIZE);
		hourMinuseTextView.setTextSize(TEXT_SIZE);
		
		minPlusTextView.setTextSize(TEXT_SIZE);
		minTextView.setTextSize(TEXT_SIZE);
		minMinuseTextView.setTextSize(TEXT_SIZE);
		
		secPlusTextView.setTextSize(TEXT_SIZE);
		secTextView.setTextSize(TEXT_SIZE);
		secMinuseTextView.setTextSize(TEXT_SIZE);
		
		settingButton.setTextSize(TEXT_SIZE);
		cencelButton.setTextSize(TEXT_SIZE);
		
		titleTextView.setTextColor(Color.WHITE);
		settingButton.setTextColor(Color.WHITE);
		cencelButton.setTextColor(Color.WHITE);
		hourPlusTextView.setTextColor(Color.BLACK);
		hourTextView.setTextColor(Color.BLACK);
		hourMinuseTextView.setTextColor(Color.BLACK);
		minPlusTextView.setTextColor(Color.BLACK);
		minTextView.setTextColor(Color.BLACK);
		minMinuseTextView.setTextColor(Color.BLACK);
		secPlusTextView.setTextColor(Color.BLACK);
		secTextView.setTextColor(Color.BLACK);
		secMinuseTextView.setTextColor(Color.BLACK);
		
		titleTextView.setText(MilkUtil.getTimeSec(hour, min, sec));
		
		hourTextView.setText(MilkUtil.getTime(hour));
		minTextView.setText(MilkUtil.getTime(min));
		secTextView.setText(MilkUtil.getTime(sec));
	}

	public MilkTimeDialog(Context context, TimeCollBank collbank, int hour, int min, int sec)
	{
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		
		this.context = context;
		this.collbank = collbank;
		this.hour = hour;
		this.min = min;
		this.sec = sec;
	}
	
	private void setSettingFontSize()
	{
		int fontSize = 9;
		
		SharedPreferences p = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int fontFlag = p.getInt(context.getString(R.string.prefFont), 1);
		
		if (fontFlag == 0) fontSize = 13;
		else if (fontFlag == 1) fontSize = 16;
		else if (fontFlag == 2) fontSize = 20;
		
		TEXT_SIZE = fontSize;
	}
	
	public interface TimeCollBank
	{
		public abstract void getTime(int hourTime, int minTime, int secTime);
	}
}
